package com.Iconcat.system.service;

import com.Iconcat.system.vo.SocketUserInfo;
import lombok.Data;
import org.springframework.stereotype.Service;

import javax.websocket.*;
import java.io.IOException;
import java.net.URI;
import java.security.Principal;
import java.util.*;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;

@Service
public class SessionService {

    private List<Session> userQueue = new ArrayList<>();
    private Map<Session, Session> userToAdminMap = new HashMap<>();
    private List<Session> adminQueue = new ArrayList<>();

    // 获取等待中的用户的会话
    public Session getQueuedUser() {
        if (!userQueue.isEmpty()) {
            return userQueue.remove(0);
        }
        return null;
    }

    // 获取可用的管理员的会话
    public Session getAvailableAdmin() {
        if (!adminQueue.isEmpty()) {
            return adminQueue.remove(0);
        }
        return null;
    }
    // 获取用户对应的管理员会话
    public Session getAdminSessionForUser(Session userSession) {
        return userToAdminMap.get(userSession);
    }

    // 发送消息给管理员
    public void sendMessageToAdmin(String message, Session adminSession) {
        sendMessage(message, adminSession);
    }
    // 发送消息给指定会话
    public void sendMessage(String message, Session session) {
        try {
            session.getBasicRemote().sendText(message);
        } catch (IOException e) {
            System.err.println("Error sending message to session " + session.getId() + ": " + e.getMessage());
        }
    }

    // 将用户添加到管理员的会话中
    public void addUserToAdminSession(String username, Session adminSession) {
        Session userSession = createNewUserSession(username);
        userToAdminMap.put(userSession, adminSession);

        sendMessageToUser(userSession, "Connected to admin");
        sendMessageToAdmin(adminSession, "User connected: " + username);
    }

    // 创建新的用户会话
    private Session createNewUserSession(String username) {
        // 实际实现可能涉及更多逻辑，例如配置用户会话的属性等
        // 这里只是一个简化的例子
        return new Session() {
            @Override
            public WebSocketContainer getContainer() {
                return null;
            }

            @Override
            public void addMessageHandler(MessageHandler handler) throws IllegalStateException {

            }

            @Override
            public Set<MessageHandler> getMessageHandlers() {
                return null;
            }

            @Override
            public void removeMessageHandler(MessageHandler listener) {

            }

            @Override
            public String getProtocolVersion() {
                return null;
            }

            @Override
            public String getNegotiatedSubprotocol() {
                return null;
            }

            @Override
            public List<Extension> getNegotiatedExtensions() {
                return null;
            }

            @Override
            public boolean isSecure() {
                return false;
            }

            @Override
            public boolean isOpen() {
                return false;
            }

            @Override
            public long getMaxIdleTimeout() {
                return 0;
            }

            @Override
            public void setMaxIdleTimeout(long timeout) {

            }

            @Override
            public void setMaxBinaryMessageBufferSize(int max) {

            }

            @Override
            public int getMaxBinaryMessageBufferSize() {
                return 0;
            }

            @Override
            public void setMaxTextMessageBufferSize(int max) {

            }

            @Override
            public int getMaxTextMessageBufferSize() {
                return 0;
            }

            @Override
            public RemoteEndpoint.Async getAsyncRemote() {
                return null;
            }

            @Override
            public RemoteEndpoint.Basic getBasicRemote() {
                return null;
            }

            @Override
            public String getId() {
                return null;
            }

            @Override
            public void close() throws IOException {

            }

            @Override
            public void close(CloseReason closeReason) throws IOException {

            }

            @Override
            public URI getRequestURI() {
                return null;
            }

            @Override
            public Map<String, List<String>> getRequestParameterMap() {
                return null;
            }

            @Override
            public String getQueryString() {
                return null;
            }

            @Override
            public Map<String, String> getPathParameters() {
                return null;
            }

            @Override
            public Map<String, Object> getUserProperties() {
                return null;
            }

            @Override
            public Principal getUserPrincipal() {
                return null;
            }

            @Override
            public Set<Session> getOpenSessions() {
                return null;
            }

            @Override
            public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Partial<T> handler) throws IllegalStateException {

            }

            @Override
            public <T> void addMessageHandler(Class<T> clazz, MessageHandler.Whole<T> handler) throws IllegalStateException {

            }
        };
    }

    // 将用户添加到排队列表
    public void addUserToQueue(String username, Session userSession) {
        userQueue.add(userSession);
        // 可以发送一些消息告知用户他们在排队中
        sendMessageToUser(userSession, "You are in the queue. Please wait.");
    }

    // 将管理员添加到排队列表
    public void addAdminToQueue(String username, Session adminSession) {
        adminQueue.add(adminSession);
        // 可以发送一些消息告知管理员他们在等待新用户连接
        sendMessageToAdmin(adminSession, "Waiting for a new user to connect.");
    }

    // 获取用户会话对应的用户名
    public String getUsername(Session userSession) {
        // 实际实现中可能需要更复杂的逻辑，这里简化为从用户到管理员的映射中查找
        for (Map.Entry<Session, Session> entry : userToAdminMap.entrySet()) {
            if (entry.getKey().equals(userSession)) {
                return "User" + entry.getKey().getId();
            }
        }
        return null;
    }

    // 发送消息给用户
    private void sendMessageToUser(Session userSession, String message) {
        // 实际实现中需要发送 WebSocket 消息给用户
        System.out.println("Sending message to user: " + message);
    }

    // 发送消息给管理员
    private void sendMessageToAdmin(Session adminSession, String message) {
        // 实际实现中需要发送 WebSocket 消息给管理员
        System.out.println("Sending message to admin: " + message);
    }

    // 其他逻辑...
}
